<template>
  <div class="system-user-container layout-padding">
    <el-card shadow="hover">
      <div class="system-menu-search mb15">
        <el-button size="default" type="success" class="ml10" @click="onOpenAddDialog">
          <el-icon>
            <plus/>
          </el-icon>
          新增文章类型
        </el-button>
      </div>
      <el-table :data="state.tableData.data" stripe style="width: 100%">

        <el-table-column label="序号" prop="id"/>

        <el-table-column label="文章类型" prop="name"/>

        <el-table-column label="是否启用" prop="isshow">
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.status">启用</el-tag>
            <el-tag type="danger" v-else>禁用</el-tag>

          </template>
        </el-table-column>

        <el-table-column label="创建时间" prop="createdAt"/>
        <el-table-column label="修改时间" prop="updatedAt"/>

        <el-table-column label="操作" width="280">
          <template #default="scope">
            <el-button size="small" type="primary" @click="onOpenEditUser('edit', scope.row)">编辑</el-button>
            <el-button
                size="small"
                type="danger"
                v-if="scope.row.status"
                @click="onUpdateStatus(scope.row, false)"
            >禁用
            </el-button>
            <el-button
                size="small"
                type="success"
                v-else
                @click="onUpdateStatus(scope.row, true)"
            >启用
            </el-button>
          </template>
        </el-table-column>

      </el-table>

      <el-pagination
          size="small"
          @size-change="onHandleSizeChange"
          @current-change="onHandleCurrentChange"
          class="mt15"
          :pager-count="5"
          :page-sizes="[10, 20, 30]"
          v-model:current-page="state.tableData.param.pageNum"
          background
          v-model:page-size="state.tableData.param.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="state.tableData.total"
      >
      </el-pagination>
    </el-card>
    <Dialog ref="DialogRef" @refresh="getTableData()"/>

  </div>
</template>

<script setup lang="ts" name="systemUser">
import {defineAsyncComponent, reactive, onMounted, ref} from "vue";
import {Plus} from "@element-plus/icons-vue";
import {getArticleType, updateArticleTypeStatus} from "/@/views/articleType/articleType";
// 引入组件
const Dialog = defineAsyncComponent(() => import("/src/views/articleType/dialog.vue"));


// 定义变量内容
const DialogRef = ref();
const state = reactive({
  keywords: "",
  mytag: 0,
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10
    }
  }
});

// 初始化表格数据
const getTableData = () => {
  state.tableData.loading = true;
  getArticleType(
      {
        page: state.tableData.param.pageNum,
        pageSize: state.tableData.param.pageSize,
      }
  ).then((res) => {
    console.log(res)
    state.tableData.data = res.articleTypes;
    state.tableData.total = res.total_count;
  });
  setTimeout(() => {
    state.tableData.loading = false;
  }, 500);
};
// 打开新增用户弹窗
const onOpenAddDialog = (type: string) => {
  DialogRef.value.openDialog(type);
};
// 打开修改用户弹窗
const onOpenEditUser = (type: string, row: any) => {
  DialogRef.value.openDialog(type, row);
};
// 更新文章类型状态（启用/禁用）
const onUpdateStatus = (row: any, status: boolean) => {
  if (status) row.status = 1; else row.status = 0
  updateArticleTypeStatus(row).then(() => {
    getTableData();
  })
};
// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
  padding: 0px;

  :deep(.el-card__body) {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: auto;

    .el-table {
      flex: 1;
    }
  }
}
</style>
